package com.noranj.web.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;



public class FormakApp implements EntryPoint {
	
	  @UiField
	  FolderList folderList;
	
	RootLayoutPanel root;
	private static FormakApp singleton;

	private final UserManagmentAsync userManagment = GWT.create(UserManagment.class);  
	
	interface FormakAppUiBinder extends UiBinder<DockLayoutPanel, FormakApp> {}
	  
	private static final FormakAppUiBinder binder = GWT.create(FormakAppUiBinder.class);
	
	public static FormakApp get() {
		return singleton;
	}
	
	 public void onModuleLoad() {
		 singleton = this;
		 doLogin();
		  }

	 private void doLogin(){
		userManagment.login(Window.Location.getPath()   , new AsyncCallback<String>() {
		      public void onFailure(Throwable caught) {
		        //Window.alert("An error occurred");
		      }

			@Override
			public void onSuccess(String result) {
				if(result.isEmpty())
				{	
					goAfterLogin();
				}
				else
				{	Window.Location.replace(result);}
				
			}

		    });
	 }
	  private void goAfterLogin() {

		    DockLayoutPanel outer = binder.createAndBindUi(this);
		    root = RootLayoutPanel.get();
		    root.clear();
		    root.add(outer);


		  }

}
